{extend name="public/base"/}
{block name="style"}
{/block}
{block name="content"}
    <div class="data-list" data-url="{:url('index')}">
        <form class="layui-form inline-form">
            <div class="pull-left">
                <div class="layui-inline">
                    <input class="layui-input input-order-sn" autocomplete="off" autofocus="autofocus" name="check_sn" placeholder="盘点单号">
                </div>
                <div class="layui-inline">
                    <input class="layui-input laydate-range" autocomplete="off" name="check_date" placeholder="盘点时间">
                </div>
                <div class="layui-inline">
                    <input class="layui-input laydate-range" autocomplete="off" name="finish_date" placeholder="盘点完成时间">
                </div>
                <div class="layui-inline">
                    <select name="status" lay-search lay-filter="data-list">
                        <option value="">盘点状态</option>
                        {foreach $status as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="check_status" lay-search lay-filter="data-list">
                        <option value="">审批状态</option>
                        {foreach $check_status as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
                </div>
            </div>
        </form>
        <div class="layui-row form-lists-button">
            <div class="layui-inline layui-btn-group">
                <button class="layui-btn layui-btn-normal layui-btn-sm ajax-form" width-form="50%" height-form="50%" data-url="{:url('add')}"><i class="layui-icon layui-icon-add-circle"></i>新增盘点单</button>
                <button id="import-fba-btn" class="layui-btn layui-btn-normal layui-btn-sm" width-form="30%" height-form="30%"><i class="layui-icon layui-icon-upload-drag"></i>导入FBA库存</button>
            </div>
        </div>
        <div class="data">
            <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
        </div>
    </div>
{/block}
{block name="script"}
<script type="text/html" id="platform-fab-excel">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">仓库</label>
            <div class="layui-input-inline">
                <select id="platform-fab" lay-search>
                    <option value="">请选择FBA仓库</option>
                    {foreach name="warehouse" id="w"}
                    <option value="{$w.warehouse_id}">{$w.name}</option>
                    {/foreach}
                </select>
                <p style="color: red; display: none;" id="platform-fab-error-msg">请选择FBA仓库</p>
            </div>
            <div class="layui-input-inline">
                <button type="button" id="import-platfrom-fab-excel" class="layui-btn layui-btn-normal"><i
                        class="layui-icon layui-icon-upload-drag"></i>选择文件
                </button>
            </div>
        </div>
    </form>
</script>
<script type="text/javascript">
layui.use(['layer', 'jquery', 'upload', 'form'], function()
{
    var layer = layui.layer
        , upload = layui.upload
        , form = layui.form
        , $ = layui.jquery;
    $("#import-fba-btn").click(function()
    {
        layer.open({
            title: '导入FBA库存表格',
            area: ['50%', '50%'],
            content: $("#platform-fab-excel").html()
            , success: function (layero, index)
            {
                form.render('select');
                var orderFile = upload.render({
                    elem: '#import-platfrom-fab-excel'
                    , url: '{:url("file/upload",["type"=>"document"])}'
                    , accept: 'file' //普通文件
                    , exts: 'xlsx|xls|csv' // 指定类型
                    , field: "file"
                    , auto: false
                    , choose: function(obj)
                    {
                        var wid = $("#platform-fab option:selected").val();
                        if(!wid)
                        {
                            $("#platform-fab-error-msg").show();
                            $("#import-platfrom-fab-excel").next().val('');
                            return;
                        }
                        orderFile.upload();
                    }
                    , before: function (obj) {
                        layer.load(0, {shade: false});
                    }
                    , done: function (response, index, upload) {
                        if (response.code != 1) {
                            layer.closeAll('loading');
                            var icon = response.code == 1 ? 6 : 5;
                            layer.msg(response.msg, {icon: icon, time: 1500});
                            return;
                        }
                        var wid = $("#platform-fab option:selected").val();
                        $.post("{:url('importFBA')}", {"warehouse_id":wid, "path": response.data.src}).done(function (response) {
                            layer.closeAll('loading');
                            var icon = 5, time = 5500;
                            if (response.code == 1) {
                                icon = 6;
                                time = 1500;
                            }
                            var icon = response.code == 1 ? 6 : 5;
                            layer.msg(response.msg, {icon, time}, function(){
                                if (response.code == 1) reloadOrderTable();
                            });
                        }).error(function(xhr)
                        {
                            layer.msg('服务器异常，请稍后重试~', {icon: 5});
                            layer.closeAll('loading');
                        });
                    }
                    , error: function (index, upload) {
                        layer.msg('上传失败', {icon: 5});
                        layer.closeAll('loading');
                    }
                });
            }
            , yes: function (index, layero)
            {
                layer.close(index);
            }
        });
    });
});
</script>
{/block}